package com.web.kongjian.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Message;
import com.web.kongjian.ao.MessageAo;

@Controller
@RequestMapping("/message")
public class MessageAction
{
	@Autowired
	private MessageAo messageAo;
	
	@RequestMapping("/sendMessage")
	public ModelAndView sendMessage(String content ,long memberId,HttpServletRequest request ,HttpServletResponse response)
	{
		String callback =  request.getParameter ("callback");
		content = content.trim ();
		
		Message message = new Message();
		message.setContent (content);
		message.setMemberId (memberId);
		
		
		messageAo.leaveMessage (message,request);
		
		if(StringUtils.isTrimEmpty (callback))
		{
			try
			{
				response.sendRedirect ("/");
			}catch (IOException e)
			{
				e.printStackTrace();
			}
		}else
		{
			try
			{
				response.sendRedirect (callback);
			}catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		return null;
	}

	@RequestMapping("/delete")
	public ModelAndView delete(HttpServletRequest request,long messageId)
	{
		messageAo.delete (messageId ,request);
	
		return null;
	}
	
	@RequestMapping("/reply")
	public ModelAndView reply(HttpServletRequest request,long memberId,String content) throws UnsupportedEncodingException
	{
		
		content = new String(content.getBytes("iso8859-1"),"gbk");
		
		messageAo.reply (request ,memberId ,content);
		return null;
	}
	
	
	@RequestMapping("/refuse")
	public ModelAndView refuse(HttpServletRequest request,long memberId)
	{
		messageAo.refuse (request ,memberId);
		
		return null;
	}
}
